<template>
  <el-card style="width: 100%;margin-bottom: 20px;">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="医院名称">
        <el-input v-model="formInline.name" placeholder="请输入医院名称" clearable />
      </el-form-item>
      <el-form-item label="医院别名">
        <el-input v-model="formInline.nickname" placeholder="请输入医院别名" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="width: 100%">
    <el-table :data="list" style="width: 100%">
      <el-table-column fixed prop="id" label="编号" width="80" />
      <el-table-column prop="name" label="医院名称" />
      <el-table-column prop="nickname" label="医院别名" />
      <el-table-column prop="address" label="医院地址" />
      <el-table-column prop="scale" label="医院规模" />
      <el-table-column prop="description" label="医院简介" />
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleClick(scope.row)">
          详情
          </el-button>
          <el-button link type="primary" size="small" @click="onEdit(scope.row)">编辑</el-button>
          <el-button link type="primary" size="small" @click="onDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <Dialog :title="titles" v-model="show">
    <el-form ref="menuFormRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="医院名称">
        <el-input v-model="formData.name" placeholder="医院名称" />
      </el-form-item>
      <el-form-item label="医院别名">
        <el-input v-model="formData.nickname" placeholder="医院别名" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number
          v-model="formData.scale"
          style="width: 100px"
          :min="0"
        />
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="formData.address" placeholder="地址" />
      </el-form-item>
      <el-form-item label="医院简介">
        <el-input  type="textarea" autosize v-model="formData.description" placeholder="医院简介" />
      </el-form-item>
      <el-form-item label="状态">
        <el-switch
          v-model="formData.status"
          inactive-value="0"
          active-value="1"
          :disabled="type == 'detail'"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
        
      </el-form-item>
    </el-form>
    <template #footer v-if="type != 'detail'">
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleCloseDialog">取 消</el-button>
      </div>
    </template>
  </Dialog>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessageBox } from 'element-plus';
import { RecruitmentHospitalApi } from '@/api/hospital/recruitment.hospital';
const formInline = reactive({
  name: '',
  nickname: '',
  pageNo: 1,
  pageSize: 10
})
const total = ref(0);
const list = ref([]);
const title = ref('');
const type = ref('');
const visbale = ref(false);
const formData = reactive({
  name: '',
  nickname: '',
  category: 1,
  scale: undefined,
  address: '',
  logoUrl: '',
  description: '',
  status: 1
})
const show = ref(false);
const titles = ref('')

onMounted(()=>{
    onLoadData();
})

const onLoadData = () => {
  RecruitmentHospitalApi.getRecruitmentHospitalPage(formInline)
    .then(res=>{
      list.value = res.list;
      total.value = res.total;
    })
}

const onSubmit = () => {
  formInline.pageNo = 1;
  onLoadData();
}
const handleClick = (data) => {
  type.value = 'detail'
  titles.value = '医院详情';
  show.value = true;
  Object.assign(formData, data);
}
const onAdd = () => {
  type.value = 'add'
  let data = {
    name: '',
    nickname: '',
    category: 1,
    scale: undefined,
    address: '',
    logoUrl: '',
    description: '',
    status: 1,
    id: undefined
  }
  Object.assign(formData, data)
  titles.value = '新增医院';
  show.value = true;
}
const handleCloseDialog = () => {
  show.value = false;
}
const onReset = () => {
  let data = {
    id: undefined,
    name: '',
    nickname: '',
    category: 1,
    scale: undefined,
    address: '',
    logoUrl: '',
    description: '',
    status: 1
  }
  Object.assign(formData, data);
}
const handleSubmit = () => {
  let params = {...formData}
  if (params.id) {
    RecruitmentHospitalApi.updateRecruitmentHospital(params)
      .then(res=>{
        show.value = false;
        onReset();
        formInline.pageNo = 1;
        onLoadData();
      })
  } else {
    RecruitmentHospitalApi.createRecruitmentHospital(params)
      .then(res=>{
        show.value = false;
        onReset();
        formInline.pageNo = 1;
        onLoadData();
      })
  }
}

const onEdit = (data) => {
  type.value = 'edit'
  titles.value = '编辑医院';
  show.value = true;
  Object.assign(formData, data);
}

const onDel = (id) => {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      RecruitmentHospitalApi.deleteRecruitmentHospital(id)
        .then(res=>{
          ElMessage.success("删除成功");
          formInline.pageNo = 1;
          onLoadData();
        })
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}
</script>
